package com.ccg.securityConfig;

import com.ccg.entity.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {

    private ObjectMapper objectMapper=new ObjectMapper();

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{

        System.out.println("===========登出成功================");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter printWriter = response.getWriter();
//        response.setHeader("Content-Type", "application/json;charset=utf-8");
        Message message = new Message(0,"登出成功",null);
        String json = objectMapper.writeValueAsString(message);

        printWriter.write(json);
        printWriter.flush();
        printWriter.close();
    }

}
